package org.tank;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
public class TankFram2 extends Frame {

    public static  final int GAME_WIDTH=800;
    public static  final int GAME_HEIGHT=800;
    List<Bullet> list=new ArrayList<>();
     Tank tank=new Tank(200,200,this,true,50,50);

      //爆炸效果
    List<Explode> explodes=new ArrayList<>();

   //多個坦克
    List<Tank> tankList=new ArrayList<>();

    Explode explode=new Explode();
  //坦克的状态
    boolean isLeft=false;
    boolean isRight=false;
    boolean isUp=false;
    boolean isDown=false;

    //子弹的装填
    boolean bl=false;
    boolean br=false;
    boolean bu=false;
    boolean bd=false;

    public TankFram2() {
        setSize(GAME_WIDTH, GAME_HEIGHT);
        setResizable(false);
        setTitle("tank war");
        setVisible(true);
        this.addKeyListener(new MykeyLister());
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    @Override
    public void paint(Graphics g) {

      //子弹数量判断
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).isLive==false){
                list.remove(i);
            }
        }
        System.out.println("paint");
        System.out.println("子弹数量+"+list.size());
        for (int i = 0; i < explodes.size(); i++) {
            Explode explode = explodes.get(i);
            if(!explode.isLive){
                explodes.remove(explode);
                continue;
            }
            explode.fillRect(g);
        }

        tank.fillRect(g,isLeft,isRight,isUp,isDown,null,50,50);
        for (int i = 0; i <list.size() ; i++) {
              list.get(i).fillRect(g);
              list.get(i).setMove(bl,br,bu,bd);
        }

        for (int i = 0; i < tankList.size(); i++) {
            Tank tank1 = tankList.get(i);
            if(!tank1.isLive){
                tankList.remove(tank1);
                continue;
            }
            tank1.fillRect(g,isLeft,isRight,isUp,isDown,Color.red,50,50);
             tank1.setMove(false,false,false,true);

        }


        // 判断坦克和子弹是不是已经形成了交界
        for (int i = 0; i < tankList.size(); i++) {
            Tank tank1 = tankList.get(i);
            Rectangle rectangle1 = new Rectangle(tank1.x, tank1.y, tank1.width, tank1.height);
            for (int j = 0; j <list. size() ; j++) {
               //子弹
                Bullet bullet = list.get(j);
                Rectangle rectangle2 = new Rectangle(bullet.x,bullet.y,bullet.width,bullet.height);
                if(rectangle1.intersects(rectangle2)){
                   tank1.isLive=false;
                   //把爆炸效果放进去
                    explodes.add(new Explode(bullet.x,bullet.y,0,0,null,true));

                }

            }
        }
    }




    class MykeyLister extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {
            int key=e.getKeyCode();
            switch (key){
                case KeyEvent.VK_LEFT:
                   isLeft=true;
                   bl=true;
                    break;
                case KeyEvent.VK_RIGHT:
                    isRight=true;
                    br=true;
                    break;
                case KeyEvent.VK_UP:
                    isUp=true;
                    bu=true;
                    break;
                case KeyEvent.VK_DOWN:
                    isDown=true;
                    bd=true;
                    break;
                case KeyEvent.VK_CONTROL:
                    bl=false;
                    br=false;
                    bu=false;
                    bd=false;

                   tank.setFire(isLeft,isRight,isUp,isDown,null);
                    break;

            }

           tank.setMove(isLeft,isRight,isUp,isDown);


            repaint();

        }



        @Override
        public void keyReleased(KeyEvent e) {
            int key=e.getKeyCode();
            switch (key){
                case KeyEvent.VK_LEFT:
                    isLeft=false;
                    break;
                case KeyEvent.VK_RIGHT:
                    isRight=false;
                    break;
                case KeyEvent.VK_UP:
                    isUp=false;
                    break;
                case KeyEvent.VK_DOWN:
                    isDown=false;
                    break;
            }
           tank.setMove(isLeft,isRight,isUp,isDown);

            repaint();

        }


    }



}
